package com.fg.cloud.agency.controller.account;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.agency.controller.BaseAgencyController;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.factory.FactoryStatementDetails;
import com.fg.cloud.manager.entry.factory.FactoryStatementEntity;
import com.fg.cloud.manager.service.factory.FactoryStatementService;


/**
  * Description:经销商端对账单 controller
  * @author shaoqiping
  * @date 2017年8月30日上午11:23:23
*/
@Controller
@RequestMapping("/ageStatement")
public class AgencyStatementController extends BaseAgencyController {

	@Autowired
	private FactoryStatementService statementService;
	
	/**
	  * Description:经销商确认对账单
	  * @param request
	  * @param response
	  * @param entity
	  * @author shaoqiping
	  * @date 2017年8月30日下午2:00:51
	*/
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("/confirmStatement")
	@ResponseBody
	public void confirmStatement(HttpServletRequest request, HttpServletResponse response, FactoryStatementEntity entity){
		try {
			if(entity==null || LongUtils.isEmpty(entity.getId())||entity.getStatus()<=0||entity.getStatus()>2){
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应对账单进行确认");
				return;
			}
			entity.setAgeId(getCurrentAgencyUser().getAgeId());
			entity.setFacId(getCurrentAgencyUser().getFacId());
			boolean flag = statementService.modify(entity);
			if(flag){
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			}else{
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	/**
	 * Description:查询对账单详情
	 * 
	 * @param request
	 * @param response
	 * @param statementEntity
	 * @author shaoqiping
	 * @date 2017年8月29日下午3:19:33
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("/findDetails")
	@ResponseBody
	public void findDetails(HttpServletRequest request, HttpServletResponse response, Long id) {
		try {
			if (id == null || LongUtils.isEmpty(id)) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应对账单");
				return;
			}
			List<FactoryStatementDetails> details = statementService.findDetails(id,
					getCurrentAgencyUser().getFacId());
			if (details == null) {
				doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, details, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:条件分页查询
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param statementEntity
	 * @author shaoqiping
	 * @date 2017年8月29日下午3:02:29
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("/findList")
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String startTime, String endTime,String statementNum,Integer status) {
		try {
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<FactoryStatementEntity> pageInfo = new PageUtils<FactoryStatementEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageInfo.getCondition();
			condition.put("facId", getCurrentAgencyUser().getFacId());
			condition.put("ageId",  getCurrentAgencyUser().getAgeId());
			condition.put("startTime",startTime);
			condition.put("endTime", endTime);
			condition.put("statementNum",statementNum);
			condition.put("status",status);
			condition.put("push",1);
			pageInfo.setCondition(condition);
			
			statementService.findList(pageInfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageInfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:查询对账单还款详情
	  * @param request
	  * @param response
	  * @param id
	  * @author shaoqiping
	  * @date 2017年10月13日下午2:39:58
	*/
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("/findPayments")
	@ResponseBody
	public void findPayments(HttpServletRequest request, HttpServletResponse response,Long id){
		try {
			if (id == null || LongUtils.isEmpty(id)) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应流水");
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, statementService.findPayments(id), ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
